<?php
namespace Admin\Model;
use Think\Model;
/**
 * 幻灯片模型
 */
 class LinksModel extends  Model{
 	
	public $_validate = array();
 	/**
	 * 幻灯片添加方法
	 */
	 public function addLink(){
		$this->_validate =array(
			array('lname','require','链接名称不能为空'),
			array('lurl','require','链接地址不能为空'),
		);
		if(!$this->create()){
			return false;
		}
		$data = $_POST;
		if($_FILES){		//如果有上传文件
			//图片上传
			if($_FILES['limg']['error'] != 4  ){
				//上传图像
				$upload = new \Think\Upload();// 实例化上传类
				$upload->maxSize   =     3145728 ;// 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
				$upload->savePath  =     'Links/big'; // 设置附件上传目录
				$upload->saveName  =    'duosute_'.date('ymdHis');
				$upload->autoSub   = 	true;
				$upload->callback  =    false;
				$upload->subName   =    false;
				$file =  $upload->upload();
				if(!$file) {// 上传错误提示错误信息        
					$this->error='上传错误';
					return false;
			    }else{
		    		$path =  'Uploads/'.$file['limg']['savepath'].$file['limg']['savename'];//原图样式  大图样式
					$thumbPath = 'Uploads/'.$file['limg']['savepath'].'thumb/'.$file['limg']['savename']; 	
					$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   
			
					$img =  new \Think\Image();
					$img->open($path);
					$img->thumb(88, 31,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
					$data['limg'] = $file['limg']['savename'];//缩图图
			    }
					
			}	
		}
		M('links')->add($data);
		return true;
	 }
	 
	 
	 
	 public function editLinks(){
	 	$lid = I('get.lid');
		$this->_validate =array(
			array('lname','require','链接名称不能为空'),
			array('lurl','require','链接地址不能为空'),
		);
		if(!$this->create()){
			return false;
		}
		$data = $_POST;
		//图片上传
		if($_FILES['limg']['error'] != 4  ){
			//上传图像
			$upload = new \Think\Upload();// 实例化上传类
			$upload->maxSize   =     3145728 ;// 设置附件上传大小
			$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
			$upload->savePath  =     'Links/big'; // 设置附件上传目录
			$upload->saveName  =    'duosute_'.date('ymdHis');
			$upload->autoSub   = 	true;
			$upload->callback  =    false;
			$upload->subName   =    false;
			$file =  $upload->upload();
			if(!$file) {// 上传错误提示错误信息        
				$this->error='上传错误';
				return false;
		    }else{
	    		$path =  'Uploads/'.$file['limg']['savepath'].$file['limg']['savename'];//原图样式  大图样式
				$thumbPath = 'Uploads/'.$file['limg']['savepath'].'thumb/'.$file['limg']['savename']; 	
				$thumbPath= str_replace('big/', '', $thumbPath); //组合路径   
		
				$img =  new \Think\Image();
				$img->open($path);
				$img->thumb(88, 31,\Think\Image::IMAGE_THUMB_FIXED)->save($thumbPath);
				$data['limg'] = $file['limg']['savename'];//缩图图
		    }
				M('links')->add($data);
				return true;
		}
			M('links')->where("lid='{$lid}'")->save($data);
			return true;
	 }
	 
 }
